# !/usr/bin/env python3
# -*- coding: utf-8 -*-
###----------------------------------------------------1、文件说明----------------------------------------------------###
'''
* 说明：制作一个软闹钟
* 时间：2021-1-30
* 文件：
* 作者：whg
* 备注：
'''
###----------------------------------------------------2、库导入----------------------------------------------------###
import time
from datetime import date
from datetime import datetime
from datetime import timedelta
###----------------------------------------------------3、参数配置----------------------------------------------------###
###----------------------------------------------------4、主体程序----------------------------------------------------###

class WTime(object):
    '''
    常用时间函数
    '''
    def __init__(self):
        '''
        时间初始化
        '''
        self.time = time.time()

    def Time(self):
        '''
        获取时间
        :return:
        '''
        self.time = time.time()

    def GetTime(self):
        '''
        获取当前的时间
        :return:
        '''
        return self.time

    def SetTime(self, year=None, month=None, day=None, hour=None, min=None, sec=None):
        '''
        修改时间
        :param year: 年
        :param month:  月
        :param day:  日
        :param hour:  时
        :param min:  分
        :param sec:  秒
        :return: 设置成功 或 失败
        '''
        localtime = list(time.localtime(self.time))
        if year:
            localtime[0] = year
        if month:
            localtime[1]  = month
        if day:
            localtime[2]  = day
        if hour:
            localtime[3]  = hour
        if min:
            localtime[4]  = min
        if sec:
            localtime[5]  = sec
        strtime = "{}-{}-{} {}:{}:{}".format(localtime[0], localtime[1], localtime[2],
                                             localtime[3], localtime[4], localtime[5])
        try:
            self.time = time.mktime(time.strptime(strtime, "%Y-%m-%d %H:%M:%S"))
            return True
        except:
            return False

    def ShowTime(self, stime=None):
        '''
        以常见的格式 返回类中的时间
        :param stime: 对外部的时间进行显示
        :return:
        '''
        if stime:
            return time.strftime("%Y-%m-%d %H:%M:%S", stime)
        else:
            return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.time))

    def GetDelayTime(self, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0, milliseconds=0):
        '''
        获取一段时间之前或之后的时间（正数为后，负数为前）
        :param weeks:
        :param days:
        :param hours:
        :param minutes:
        :param seconds:
        :param microseconds:
        :param milliseconds:
        :return:
        '''
        nowday = datetime.fromtimestamp(self.time)
        lastday = nowday + timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes,
                                     seconds=seconds, microseconds=microseconds, milliseconds=milliseconds)
        return self.ShowTime(lastday.timetuple())

    def SetDelayTime(self, weeks=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0, milliseconds=0):
        '''
        设置一段时间之前或之后的时间（正数为后，负数为前）
        :param weeks:
        :param days:
        :param hours:
        :param minutes:
        :param seconds:
        :param microseconds:
        :param milliseconds:
        :return:
        '''
        nowday = datetime.fromtimestamp(self.time)
        lastday = nowday + timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes,
                                     seconds=seconds, microseconds=microseconds, milliseconds=milliseconds)
        self.time = lastday.timestamp()

if __name__ == '__main__':
    print('Start Program...\r\n')
    stime = WTime()
    print(stime.GetTime())

    print(stime.ShowTime())
    # print(stime.SetTime(day=19))
    # print(stime.ShowTime())
    #
    # print(stime.GetDelayTime(days=28))
    print(stime.SetDelayTime(days=0))
    print(stime.ShowTime())
    print('\nSuccess ...')
